﻿// Would like a plugin where you enumerate a file name and a maximum age in hours.   
// If the age limit is exceeded or the file does not exist report as failure.
namespace Wolfpack.Contrib.Checks.FileAlarms
{
    using System;
    using System.IO;
    using Wolfpack.Core;
    using Wolfpack.Core.Interfaces;
    using Wolfpack.Core.Interfaces.Entities;

    public class FileAlarmConfig : PluginConfigBase
    {
        public string FileLocation { get; set; }

        public string MaxAge { get; set; }
    }

    public class FileAlarm : IHealthCheckPlugin
    {
        /// <summary>
        /// default ctor
        /// </summary>
        public FileAlarm(FileAlarmConfig config)
        {
            this.MyConfig = config;
            this.MyFileLocation = new SmartLocation(config.FileLocation);
            this.MyIdentity = new PluginDescriptor
            {
                Description = string.Format("Reports information about file {0}", this.MyFileLocation.Location),
                TypeId = new Guid("8E15D923-9884-4920-A489-86956EECCDB7"),
                Name = this.MyConfig.FriendlyId
            };
        }

        public FileAlarmConfig MyConfig { get; set; }

        public PluginDescriptor MyIdentity { get; set; }

        public SmartLocation MyFileLocation { get; set; }

        public Status Status { get; set; }

        public PluginDescriptor Identity
        {
            get { return this.MyIdentity; }
        }

        public void Initialise()
        {
            // do nothing
        }

        public void Execute()
        {
            var fi = new FileInfo(this.MyFileLocation.Location);

            bool result = true;

            if (!fi.Exists || !((DateTime.Now - (new TimeSpan(int.Parse(this.MyConfig.MaxAge), 0, 0))) < fi.LastWriteTime))
            {
                result = false;
            }

            HealthCheckData healthCheckData = new HealthCheckData
            {
                Identity = this.Identity,
                Info = string.Format("Information about file '{0}'...", this.MyFileLocation.Location),
                Result = result,
            };

            if (fi.Exists)
            {
                    healthCheckData.ResultCount = fi.Length;
                    healthCheckData.Tags = "File exists.";
                    
                    healthCheckData.Properties = new ResultProperties
                                        {
                                            { "CreationTimeUtc", fi.CreationTimeUtc.ToString() },
                                            { "LastAccessTimeUtc", fi.LastAccessTimeUtc.ToString() },
                                            { "LastWriteTimeUtc", fi.LastWriteTimeUtc.ToString() },
                                            { "Length", fi.Length.ToString() },
                                            { "Attributes", fi.Attributes.ToString() }
                                        };
            }
            else
            {
                healthCheckData.Tags = "File does not exist.";
                healthCheckData.ResultCount = 0;
            }

            Messenger.Publish(healthCheckData);

            Logger.Debug("Done checking FileAlarms.");
        }
    }
}